/*
 * Copyright (c) 2009, Katholieke Universiteit Leuven
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright notice,
 *       this list of conditions and the following disclaimer in the documentation
 *       and/or other materials provided with the distribution.
 *     * Neither the name of the Katholieke Universiteit Leuven nor the names of
 *       its contributors may be used to endorse or promote products derived from
 *       this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
/**
 * @addtogroup components
 * @{
 */
/**
 * @file
 * Implementation for the core LooCI component system
 * @author
 * Wouter Horré <wouter.horre@cs.kuleuven.be>
 */

#include "components.h"
#include "components_private.h"
#include "lib/list.h"
#include <string.h>

#ifdef WITH_LOADABLE_COMPONENTS
#include "cfs.h"
#include "elfloader.h"
#endif // WITH_LOADABLE_COMPONENTS

#ifdef LOOCI_COMPONENTS_DEBUG
#include <stdio.h>
#ifdef CONTIKI_TARGET_AVR_RAVEN
#include <avr/pgmspace.h>
#define PRINTF(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
#else
#define PRINTF printf
#endif // CONTIKI_TARGET_AVR_RAVEN
#else
#define PRINTF(...)
#endif

LIST(components);

static u8_t nextid = 1;

static void start_components(struct component * const * components)
{
  int i;
  PRINTF("Got some components to start\r\n");
  for(i=0; components[i] != NULL; ++i) {
    PRINTF("Starting a component\r\n");
    u8_t id = components_add(components[i]);
    components_start(id);
  }
}

PROCESS(looci_component_manager, "LooCI Component Manager");

PROCESS_THREAD(looci_component_manager, ev, data)
{
  PROCESS_BEGIN();

  // Initialize components
  list_init(components);

  // autostart components
  start_components(autostart_components);

  // TODO react to events for starting components etc.
  PROCESS_END();
}

struct component * components_get(u8_t id) {
  struct component * current;
  for(current = (struct component *) list_head(components); current != NULL; current = current->next) {
    if(current->id == id) {
      return current;
    }
  }
  return NULL;
}

struct component * components_get_first() {
  return (struct component *) list_head(components);
}

struct component * components_get_by_process(struct process * process) {
  struct component * current;
  for(current = (struct component *) list_head(components); current != NULL; current = current->next) {
    if(current->process == process) {
      return current;
    }
  }
  return NULL;
}

struct component * components_get_by_type(char * type) {
  struct component * current;
  for(current = (struct component *) list_head(components); current != NULL; current = current->next) {
    if(strcmp(COMPONENT_TYPE(current), type)==0) {
      return current;
    }
  }
  return NULL;
}

u8_t components_add(struct component * component) {
  // determine id
  if(component->id == COMPONENT_ID_NONE) {
    // @TODO This may wrap around ...
    component->id = nextid++;
  }
  // print some debug info
  PRINTF("Adding component '%s' with id %u\r\n", COMPONENT_TYPE(component), component->id);
  // add to list of started components
  list_add(components, component);
  return component->id;
}

void components_remove(u8_t cid) {
  struct component * component = components_get(cid);
  if(component == NULL) return;
  PRINTF("Removing component '%s' with id %u\r\n", COMPONENT_TYPE(component), component->id);
  list_remove(components, component);
}

void components_start(u8_t cid) {
  // get the component
  struct component * component = components_get(cid);
  if(component == NULL) return;
  PRINTF("Starting component '%s' with id %u\r\n", COMPONENT_TYPE(component), component->id);
  component->state = COMPONENT_STATE_ACTIVE;
  // start process
  process_start(component->process, NULL);
}

void components_stop(u8_t cid) {
  // get the component
  struct component * component = components_get(cid);
  if(component == NULL) return;
  PRINTF("Stopping component '%s' with id %u\r\n", COMPONENT_TYPE(component), component->id);
  component->state = COMPONENT_STATE_DEACTIVATED;
  // stop the process
  process_exit(component->process);
}

/** @} */

